<template>
  <div class="app-container">


    <el-row>
      <el-col :span="12">

        <el-input v-model="searchForm.menuName" />
      </el-col>
      <el-col :span="12">

        <el-button type="primary" @click="search()" size="mini">查询</el-button>
        <el-button type="primary" @click="toAdd()" size="mini">添加一级菜单</el-button>
      </el-col>
    </el-row>

    <!-- 向子组件传递数据 -->
    <el-table :data="list" v-loading="loading" row-key="menuId" border lazy
              :tree-props="{children: 'children', hasChildren: 'hasChildren'}" :load="load" size="mini"
              style="width: 100%">
<!--      <el-table-column prop="menuId" label="菜单ID" align="center" width="180"> </el-table-column>-->
      <el-table-column align="center" type="index" label="序号" >  </el-table-column>
      <el-table-column prop="menuName" label="菜单名称" align="center"> </el-table-column>
      <el-table-column prop="url" label="页面路径" align="center"> </el-table-column>
<!--      <el-table-column prop="active" label="是否有效" align="center"> </el-table-column>-->
      <el-table-column prop="createtime" label="createtime" align="center"> </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="primary" @click="toAdd(scope.row.menuId)" size="mini">添加子菜单菜单</el-button>
          <el-button type="primary" size="mini" @click="toEdit(scope.row.menuId)">修改</el-button>
        </template>
      </el-table-column>

    </el-table>

    <el-dialog :visible.sync="visible" :title="dialogTitle">

      <Edit v-if="visible" :visible.sync="visible" @close="close" :id="id" :parentId="parentId"></Edit>

    </el-dialog>




  </div>
</template>

<script>
import { getList ,deleteById ,getParent} from '@/api/sys/menu'
import Edit from  './edit.vue'
export default {
  components:{Edit},
  data() {
    return {
      dialogTitle:'',
      parentId:null,
      searchForm:{
        pageNo:1,
        size:10,
        total:0,
        dname:''
      },
      id:'',
      visible:false,
      list: null,
      listLoading: true
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    close(){
      this.fetchData()
    },
    toEdit(id){
      this.dialogTitle ="修改菜单";
      this.visible = true;
      this.id = id
    },
    toAdd(id){
      this.dialogTitle ="添加菜单";
      this.visible = true;
      this.id = null;
      if(id){
        this.parentId = id
      }else{
        this.parentId  = null;
      }


    },
    load(treeNode, tree, resolve){
      this.loading = true;
      //查询顶层菜单
      let param = {parentId:treeNode.menuId};
      getParent(param).then(list => {
        resolve(list);//渲染子菜单方法
        this.loading = false;
      })
    },
    fetchData() {
      this.loading = true;
      //查询顶层菜单
      let param = {abc:1}
      getParent(param).then(list => {
          this.list = list;
          this.loading = false;
      }).catch(e=>{
        console.log("err",e)
      })
    },
    deleteById(id){
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
          deleteById(id).then(success=>{
            if(success){
              this.$message.success("保存成功");
              this.fetchData()//加载列表
            }else{
              this.$message.error("保存失败");
            }
          });
      })

    }
  }
}
</script>
